package com.vitabuybuy.tv.ui.mine.group

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentMyGroupBuyChildBinding
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.ui.mine.group.adapter.MyClusterListAdapter
import com.vitabuybuy.tv.ui.mine.group.viewmodel.MyGroupVieModel
import kotlinx.android.synthetic.main.fragment_my_group_buy_child.*

class ClusterFragment : BaseFragment<MyGroupVieModel, FragmentMyGroupBuyChildBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_my_group_buy_child
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private val myClusterListAdapter by lazy { MyClusterListAdapter() }
    private var pageNumber = 1
    private var status = "underway"
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
        mTabEntities.add(TabEntity(getString(R.string.wait_become_group), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.not_become_group), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.has_become_group), 0, 0))
        mTabLayout_1.setTabData(mTabEntities)
        listView.apply {
            adapter = myClusterListAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun lazyLoadData() {
        viewModel.getOrderList(getString(R.string.normal_group_buy), status, pageNumber)
    }

    override fun observeData() {
        viewModel.goodslist.observe(this, Observer {
            if (pageNumber == 1) {
                myClusterListAdapter.setNewData(it)
            } else {
                myClusterListAdapter.addData(it)
            }
            if (myClusterListAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun eventHandling() {
        mTabLayout_1.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                pageNumber = 1
                when (position) {
                    0 -> status = "underway"
                    1 -> status = "failure"
                    2 -> status = "formed"
                }
                viewModel.getOrderList(getString(R.string.normal_group_buy), status, pageNumber)
            }

            override fun onTabReselect(position: Int) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getOrderList(getString(R.string.normal_group_buy), status, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getOrderList(getString(R.string.normal_group_buy), status, pageNumber)
            }
        })
        myClusterListAdapter.setOnItemClickListener { _, item, _ ->
            MyGroupBuyDetailActivity.openActivity(mContext,item.id,1)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when(msg.code){
            1 ->refresh.onLoad(msg.arg1)
        }
    }
}
